package com.zr.inquiry.controller;

import com.zr.inquiry.model.*;
import com.zr.inquiry.model.vo.*;
import com.zr.inquiry.service.InquiryService;
import com.zr.util.AllRecords;
import com.zr.util.ResultVO;
import com.zr.util.ResultVOBuilder;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2019/1/7.
 */
@RestController
public class InquiryController {

    //    "type","isTax","currencyCode"
    //状态来源
    public final static String TYPE = "type";
    public final static String CURRENCYCODE = "currencyCode";
    public final static String ISTAX = "isTax";
    public final static String INQUIRYSTATUS = "inquiryStatus";

    @Autowired
    private InquiryService inquiryService;

    @CrossOrigin
    @GetMapping("partner/queryByLegalPerson")
    public ResultVO<List<Supplier>> getSupplierName(Integer legalPersonId){
        return inquiryService.getSupplierName(legalPersonId);
    }

    @PostMapping("procurement/getPullDownList")
    @CrossOrigin
    public ResultVO<Map<String, List<KeyValueVO>>> getPullDownList(@RequestBody List<String> stringList){
        //定义了一个空的数组集合
        ArrayList<String> optionNames = new ArrayList<String>();
        //定义我们都有哪些下拉框
        optionNames.add(InquiryController.TYPE);
        optionNames.add(InquiryController.CURRENCYCODE);
        optionNames.add(InquiryController.ISTAX);
        optionNames.add(InquiryController.INQUIRYSTATUS);
        //把这些下拉框传入到一个方法中,把所有的下拉框key和value赋值
        Map<String, List<KeyValueVO>> map = getOptionMap(optionNames);
        //创建一个空的map
        Map<String, List<KeyValueVO>> newMap = new HashMap<>();
        //将需要的下拉框获取到返回给页面。
        for (String pullDownName : stringList) {
            newMap.put(pullDownName, map.get(pullDownName));
        }
        return ResultVOBuilder.success(newMap);
    }



    public Map<String, List<KeyValueVO>> getOptionMap(ArrayList<String> optionNames) {

        Map<String, List<KeyValueVO>> keyValueMap = new HashMap();
        //询价状态
        if (optionNames.contains(INQUIRYSTATUS)) {
            List<KeyValueVO> roshPropertyList = new ArrayList<>();
            for (int i = 0; i< EnumInquiryStatus.values().length; i++){
                KeyValueVO kv = new KeyValueVO(i, EnumInquiryStatus.getName(i));
                roshPropertyList.add(kv);
            }
            keyValueMap.put(INQUIRYSTATUS, roshPropertyList);
        }
        //类型
        if (optionNames.contains(TYPE)) {
            List<KeyValueVO> roshPropertyList = new ArrayList<>();
            for (int i = 0; i< EnumInquiryType.values().length; i++){
                KeyValueVO kv = new KeyValueVO(i, EnumInquiryType.getName(i));
                roshPropertyList.add(kv);
            }
            keyValueMap.put(TYPE, roshPropertyList);
        }
        //币别
        if (optionNames.contains(CURRENCYCODE)) {
            List<KeyValueVO> roshPropertyList = new ArrayList<>();
            for (int i = 0; i< EnumInquiryCurrency.values().length; i++){
                KeyValueVO kv = new KeyValueVO(i, EnumInquiryCurrency.getName(i));
                roshPropertyList.add(kv);
            }
            keyValueMap.put(CURRENCYCODE, roshPropertyList);
        }
        //是否含税
        if (optionNames.contains(ISTAX)) {
            List<KeyValueVO> roshPropertyList = new ArrayList<>();
            for (int i=0;i<EnumInquiryIsTax.values().length;i++){
                KeyValueVO kv = new KeyValueVO(i, EnumInquiryIsTax.getName(i));
                roshPropertyList.add(kv);
            }
            keyValueMap.put(ISTAX, roshPropertyList);
        }
        return keyValueMap;
    }

    @PostMapping("inquiryList/selectMaterial")
    @CrossOrigin
    public ResultVO<AllRecords> selectMaterial(@RequestBody SelectMaterialVo selectMaterialVo){

        return inquiryService.selectMaterial(selectMaterialVo);
    }
    @PostMapping("material/searchPartnerPage")
    @CrossOrigin
    public ResultVO<AllRecords> searchPartnerPage(@RequestBody SupplierSelectVo supplierSelectVo){

        return inquiryService.searchPartnerPage(supplierSelectVo);
    }

    @PostMapping("inquiryList/add")
    @CrossOrigin
    public ResultVO<List<InquiryAddVo>> addInquiry(@RequestBody @Valid InquiryAddVo inquiryAddVo, BindingResult bindingResult){

        System.out.println("controller----------inquiryAddVo------------>"+inquiryAddVo);
        if (bindingResult.hasErrors()){
            return ResultVOBuilder.error("500",bindingResult.getFieldError().getDefaultMessage());
        }
        return inquiryService.addInquiry(inquiryAddVo);
    }

    @PostMapping("inquiryList/search")
    @CrossOrigin
    public ResultVO<AllRecords> inquiryQueryPage(@RequestBody InquiryListSelectVo inquiryListSelectVo){
        return inquiryService.inquiryQueryPage(inquiryListSelectVo);
    }

    @PostMapping("inquirySheet/see")
    @CrossOrigin
    public ResultVO inquirySheetSee(@RequestBody InquirySeeVo inquirySeeVo){
        return inquiryService.inquirySheetSee(inquirySeeVo);
    }
    //修改备件明细
    @PostMapping("inquiryList/update")
    @CrossOrigin
    public ResultVO<InquiryUpdateVo> inquiryUpdateByRequestForQuotationId(@RequestBody @Valid  InquiryUpdateVo inquiryUpdateVo,BindingResult bindingResult){
        if (bindingResult.hasErrors()){
            return ResultVOBuilder.error("500",bindingResult.getFieldError().getDefaultMessage());
        }
        return inquiryService.inquiryUpdateByRequestForQuotationId(inquiryUpdateVo);
    }
    //审核
    @PostMapping("inquiryAuditor/update")
    @CrossOrigin
    public ResultVO inquiryUpdateShenke(@RequestBody @Valid  InquiryUpdateShenheStatusVo inquiryUpdateVo,BindingResult bindingResult){
        if (bindingResult.hasErrors()){
            return ResultVOBuilder.error("500",bindingResult.getFieldError().getDefaultMessage());
        }
        return inquiryService.inquiryUpdateShenke(inquiryUpdateVo);
    }
    //审批
    @PostMapping("inquiryApprover/update")
    @CrossOrigin
    public ResultVO inquiryUpdateShenPi(@RequestBody @Valid  InquiryUpdateShenPiStatusVo inquiryUpdateVo,BindingResult bindingResult){
        if (bindingResult.hasErrors()){
            return ResultVOBuilder.error("500",bindingResult.getFieldError().getDefaultMessage());
        }
        return inquiryService.inquiryUpdateShenPi(inquiryUpdateVo);
    }
    //作废
    @PostMapping("inquiryList/modifyState")
    @CrossOrigin
    public ResultVO inquiryUpdateZuofei(@RequestBody @Valid  InquiryUpdateZuofeiVo inquiryUpdateVo,BindingResult bindingResult){
        if (bindingResult.hasErrors()){
            return ResultVOBuilder.error("500",bindingResult.getFieldError().getDefaultMessage());
        }
        return inquiryService.inquiryUpdateZuofei(inquiryUpdateVo);
    }
}
